package br.kanban.view.mb;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.DragDropEvent;

import br.kanban.core.busines.HistoricoBO;
import br.kanban.core.persistence.Tarefa;
import br.kanban.view.service.Identity;

@Named
@SessionScoped
public class QuadroMB implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Inject
	private QuadroService quadroService;
	
	@Inject
	private Identity identity;
	
	@Inject
	private HistoricoBO historicoBO;
	
	public void delegar(DragDropEvent ddEvent){
		Tarefa tarefa = ((Tarefa) ddEvent.getData());  
		delegar(tarefa);
	}

	public void reabrir(DragDropEvent ddEvent){
		Tarefa tarefa = ((Tarefa) ddEvent.getData());  
		reabrir(tarefa);
	}

	public void finalizar(DragDropEvent ddEvent){
		Tarefa tarefa = ((Tarefa) ddEvent.getData());  
		if(quadroService.getTarefasPendente().contains(tarefa)){
			if(tarefa.getUsuario().equals(identity.getUsuarioLogado()))
				finalizar(tarefa);
			else
				FacesContext.getCurrentInstance().addMessage(null, 
						new FacesMessage(FacesMessage.SEVERITY_ERROR, "Você não tem permissão para finalizar esta tarefa.", "")); 			
		}else{
			finalizar(tarefa);
		}
	}
		
	public void finalizar(Tarefa tarefa){
		quadroService.finalizar(tarefa, identity.getUsuarioLogado());
	}
		
	public void delegar(Tarefa tarefa){
		quadroService.delegar(identity.getUsuarioLogado(), tarefa);
	}
		
	public void reabrir(Tarefa tarefa){
		quadroService.reabrir(historicoBO.findUsuarioFromTarefa(tarefa), tarefa);
	}
		
}
